package vip.xiaonuo.smzq.modular.kcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.core.tools.file.PathUtils;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Wjglb;
import vip.xiaonuo.smzq.modular.kcl.entity.Zsk;
import vip.xiaonuo.smzq.modular.kcl.entity.param.ZskQuery;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.ZskResult;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.ZskXq;
import vip.xiaonuo.smzq.modular.kcl.mapper.WjglbMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.ZskMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IZskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 知识库 服务实现类
 *
 * @author sqq
 * @since 2024-04-01
 */
@Service
public class ZskServiceImpl extends ServiceImpl<ZskMapper, Zsk> implements IZskService {

    @Resource
    private ZskMapper zskMapper;

    @Resource
    private WjglbMapper wjglbMapper;
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;
    @Autowired
    protected HttpServletResponse response;

    @Override
    public Page<ZskXq> page(long current, long size, ZskXq zsk) {
        Page<Zsk> page = new Page<>();

//        page.setCurrent(CommonPageRequest.defaultPage().getCurrent());
//        page.setSize(CommonPageRequest.defaultPage().getSize());
        page.setCurrent(current);
        page.setSize(size);
        Page<ZskXq> returnPage = new Page<>();
        List<ZskXq> zskXqList = new ArrayList<>();
        QueryWrapper<Zsk> queryWrapper = new QueryWrapper<>();

// 其他条件直接链式调用，表示与xzqh条件为AND关系
        if (zsk.getWjmc() != null) {
            queryWrapper.like("wjmc", zsk.getWjmc());
        }

        if (zsk.getFbsj() != null) {
            queryWrapper.like("fbsj", zsk.getFbsj());
        }

        if (zsk.getWjlx() != null) {
            queryWrapper.eq("wjlx", zsk.getWjlx());
        }

// 处理xzqh的条件，与上面的条件形成AND关系
        if (zsk.getXzqh() != null) {
            String xzqh = zsk.getXzqh().replaceAll("0+$", "");
            queryWrapper.and(i -> {
                i.like("xzqh_sheng", xzqh)
                        .or().like("xzqh_shi", xzqh)
                        .or().like("xzqh_xian", xzqh);
            });
        }

        page = zskMapper.selectPage(page, queryWrapper);
        List<Zsk> zskList = page.getRecords();
        for (Zsk zskNew : zskList) {
            ZskXq zskXq = new ZskXq();
            String fileId = wjglbMapper.selectFileId(zskNew.getPkid());
            BeanUtil.copyProperties(zskNew, zskXq);
            zskXq.setFileId(fileId);
            if (zskNew.getXzqhSheng() != null) {
                zskXq.setShengMc(getXzqh(zskNew.getXzqhSheng()));
            }
            if (zskNew.getXzqhShi() != null) {
                zskXq.setShiMc(getXzqh(zskNew.getXzqhShi()));
            }
            if (zskNew.getXzqhXian() != null) {
                zskXq.setXianMc(getXzqh(zskNew.getXzqhXian()));
            }
            zskXqList.add(zskXq);

        }
        returnPage.setCurrent(page.getCurrent());
        returnPage.setSize(page.getSize());
        returnPage.setRecords(zskXqList);
        returnPage.setTotal(page.getTotal());
        return returnPage;
    }

    @Override
    public Page<ZskResult> page(long current, long size, ZskQuery zskQuery) {
        //根据用户id获取登陆者收藏的知识库文件
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        //先查询登录这收藏过的知识库文件
        List<String> zskIds=zskMapper.getZskIds(saBaseLoginUser.getId());
        List<ZskResult> zskResultList = new ArrayList<>();
        Page<ZskResult> returnPage = new Page<>();

        Page<Zsk> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);

        LambdaQueryWrapper<Zsk> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (zskQuery.getFileType()!=null && !zskQuery.getFileType().equals("")){
            lambdaQueryWrapper.eq(Zsk::getWjlx, Integer.valueOf(zskQuery.getFileType()));

        }
        lambdaQueryWrapper
                .eq(zskQuery.getWjjb() != null, Zsk::getWjjb, zskQuery.getWjjb())
                .like(zskQuery.getWjmc() != null, Zsk::getWjmc, zskQuery.getWjmc())
                .between(zskQuery.getStartTime() != null && zskQuery.getEndTime() != null, Zsk::getFbsj, zskQuery.getStartTime(), zskQuery.getEndTime());

        page = zskMapper.selectPage(page, lambdaQueryWrapper);
        List<Zsk> zskList = page.getRecords();
        //组织文件id
        for (Zsk zskNew : zskList) {
            ZskResult zskResult = new ZskResult();
            String fileId = wjglbMapper.selectFileId(zskNew.getPkid());
            BeanUtil.copyProperties(zskNew, zskResult);
            zskResult.setFileId(fileId);
            zskResult.setIsCollection(false);
            if (zskIds!=null&&zskIds.size()>0){
                if (zskIds.contains(zskNew.getPkid())){
                    zskResult.setIsCollection(true);
                }
            }
            zskResultList.add(zskResult);
        }

        returnPage.setCurrent(page.getCurrent());
        returnPage.setSize(page.getSize());
        returnPage.setRecords(zskResultList);
        returnPage.setTotal(page.getTotal());
        return returnPage;
    }

    public String getXzqh(String dm) {
        QueryWrapper<Cb08Xzqh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dm", Integer.valueOf(dm));
        Cb08Xzqh cb08Xzqh = cb08XzqhMapper.selectOne(queryWrapper);
        if (cb08Xzqh == null) {
            return "";
        }
        return cb08Xzqh.getMc();
    }


    @Override
    public void zskFileUpload(ZskXq zsk) {

        //设置文件清单主键id
        String uuid = UUID.randomUUID().toString();
        zsk.setPkid(uuid);
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        String fbr = saBaseLoginUser.getName();
        zsk.setFbr(fbr);

        if (zsk.getFileId() != null && !zsk.getFileId().equals("")) {
            Wjglb wjglb = wjglbMapper.selectById(zsk.getFileId());
            if (wjglb != null) {
                wjglb.setZlscId(uuid);
                wjglbMapper.updateById(wjglb);
            }


        }
        Zsk newZsk = new Zsk();
        BeanUtil.copyProperties(zsk, newZsk);
        zskMapper.insert(newZsk);


    }

    @Override
    public void zskFileEdit(ZskXq zsk) {
        String time = DateUtil.format(new Date(), "YYYY-MM-dd");
        zsk.setPkid(zsk.getPkid());
        zsk.setFbsj(time);
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        String fbr = saBaseLoginUser.getName();
        zsk.setFbr(fbr);

        if (zsk.getFileId() != null && !zsk.getFileId().equals("")) {
            Wjglb wjglb = wjglbMapper.selectById(zsk.getFileId());

            QueryWrapper<Wjglb> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("zlsc_id", zsk.getPkid());
            wjglbMapper.delete(queryWrapper);
            if (wjglb != null) {
                wjglb.setZlscId(zsk.getPkid());
                Wjglb wjglbNew = wjglbMapper.selectById(wjglb.getPkid());
                if (wjglbNew == null) {
                    wjglbMapper.insert(wjglb);
                } else {
                    wjglbMapper.updateById(wjglb);
                }


            }
        }
        Zsk newZsk = new Zsk();
        BeanUtil.copyProperties(zsk, newZsk);
        zskMapper.updateById(newZsk);
    }

    @Override
    public Zsk getDetails(String pkid) {
        return zskMapper.selectById(pkid);
    }

    @Override
    public void delecte(String pkid) {
        //删除对应文件信息
        wjglbMapper.delete(new QueryWrapper<Wjglb>().eq("zlsc_id", pkid));
        //删除对应节点
        zskMapper.deleteById(pkid);
    }

    @Override
    public void fileShow(String fileId) {
        Wjglb wjglb = wjglbMapper.selectById(fileId);
            PathUtils.singleFileShow(wjglb.getWjlj(), response);

    }
}
